/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.subareas.settings;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JFileChooser;

import programsettings.ProgramSettings;
import softstarter.filehandling.FileCentre;
import softstarter.filehandling.FolderFilter;
import softstarter.filehandling.indexer.FileIndexer;
import softstarter.gui.master.GUIStateController;
import softstarter.gui.subareas.settings.itemlist.EditableRunnableItem;


public class SettingsViewController {

    private SettingsView view;
    private FileIndexer indexer;
    private GUIStateController masterController;
    
    public SettingsViewController(SettingsView view, GUIStateController masterController) {
        this.view = view;
        this.masterController = masterController;
        indexer = new FileIndexer();
    }
    
    public void saveChangesToFiles() {
    	
    	for(EditableRunnableItem item : view.getEditableRunnableItemList().getEditableRunnableItems()) {
    		FileCentre.getInstance().renameFile(item.getItem(), item.getValueFromNameField());
    		FileCentre.getInstance().changeRunnableItemPath(item.getItem(), item.getValueFromPathField());
    	}
    	
        view.buildListWithFilesAndRemoveButtons();
        view.showBasicButtons();
    }
    
    public void rejectChangesToFiles() {
    	
    	view.buildListWithFilesAndRemoveButtons();
    	view.showBasicButtons();
    }
    
    public void removeOldFiles() {
    	FileCentre.getInstance().removeOldFilesFromList();
    	view.buildListWithFilesAndRemoveButtons();
    }
    
    public void removeAllFiles() {
    	FileCentre.getInstance().removeAllFiles();
    	view.buildListWithFilesAndRemoveButtons();
    }

	public void indexAFolder() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileFilter(new FolderFilter());
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int returnVal = fileChooser.showOpenDialog(view.getView());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            FileCentre.getInstance().addJavaFiles(indexer.getAllFilesInFolder(fileChooser.getSelectedFile()));
            view.buildListWithFilesAndRemoveButtons();
        } else {
        	System.out.println("Cancelled");
        }
	}

	public void setDecoration(boolean decorated) {

		ProgramSettings.getInstance().setShowWindowDecorations(decorated);
	}

	public void setAlwaysOnTOp(boolean alwaysOnTop) {

		ProgramSettings.getInstance().setAlwaysOnTop(alwaysOnTop);
		masterController.setAlwaysOnTop(alwaysOnTop);
	}

	public void openFeedbackForm() {

		try {
			Desktop.getDesktop().browse(new URI("https://spreadsheets.google.com/spreadsheet/viewform?hl=en_US&formkey=dFMzcGFMejNhcUVmaE9wRjIzb1NQS3c6MQ#gid=0"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		
	}

	public void openHomePage() {

		try {
			Desktop.getDesktop().browse(new URI("http://cs.uef.fi/~avilja/homepage/"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		
	}
	
}
